#include <deque>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;

int cmp(string& a, string& b)
{
    if (a.size() != b.size())
	return -1;
    int res = 0;
    for (int i = 0; i < a.size(); ++i) {
	if (a[i] != b[i])
	    ++res;
    }
    return res;
}

int ladderLength(string beginWord, string endWord, vector<string>& wordList)
{
    if (beginWord.size() == 0)
	return 0;
    deque<string*> req;
    int res = 0;
    int tempres = 0;
    req.push_back(&beginWord);
    while (!req.empty()) {
	string* p = req.front();
	if (*p == endWord) {
	    return res;
	}
	req.pop_front();
	for (int i = 0; i < wordList.size(); ++i) {
	    if (cmp(*p, wordList[i]) == 1) {
		req.push_back(&wordList[i]);
	    }
	    ++tempres;
	    if (tempres == pow(wordList.size(),res+1)) {
		++res;
		tempres = 0;
	    }
	}
    }
    return -1;
}

int main(int argc, char* argv[])
{
    vector<string> wl = { "hot", "dot", "dog", "lot", "log", "cog" };
    string a = "hit";
    string b = "cog";
    cout << ladderLength(a, b, wl) << endl;
    return 0;
}
